home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2462 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer Conversion
  5. Date: 21 Jan 1996 17:51:30 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <TANMOY.96Jan21105130@qcd.lanl.gov>
  9. References: <4ds4jq$fo4@su3.in.net> <20JAN199622465412@erich.triumf.ca>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: bennett@erich.triumf.ca's message of 20 Jan 1996 22:46 PST
  14.  
  15. --text follows this line--
  16. In article <20JAN199622465412@erich.triumf.ca> bennett@erich.triumf.ca
  17. (P.Bennett) writes: 
  18. <snip>
  19.    >char *my_strcat(const char *a, const char *b)
  20.    >{
  21.    >    char done[1024];
  22. <snip>
  23.    >    return done; /* this is the suspicious pointer conversion error */
  24.    >}
  25.  
  26.    Your function is declared to return a char pointer, and you are returning an
  27.    array, which is not the same thing.  Sometimes char array names act like char
  28.    pointers (particularly when used as function parameters), but not here.
  29.  
  30.  
  31. You are correct in stating that an array is not a pointer, but in this
  32. context, the _value_ of an array is being returned. Except as an
  33. immediate operand of sizeof or &, an array always decays to its value
  34. which is a pointer to its first element. So, at this point, the return
  35. value is of the correct type.
  36.  
  37.  
  38.    A more important problem here is that "done" is an automatic array which will
  39.    cease to exist when the function returns, possibly being over-written on the
  40.    next call to another function.
  41.  
  42. This is the actual problem.
  43.  
  44.    You could declare "done" as a char pointer, and malloc() some memory for it to
  45.    point to - this would solve both problems, but the calling function would have
  46.    to free() that malloc()ed memory sometime.
  47.  
  48. Correct.
  49.  
  50.    You could also declare done as a static array (just add "static" before the
  51.    present declaration), then the array will exist for the life of the program.
  52.    I _think_ (and I know someone will correct me if I'm wrong) that changeing
  53.    "return done;" to "return &done;" will fix the "suspicious pointer conversion"
  54.    error.
  55.  
  56. Wrong. You can use static arrays, but &done has the type pointer to
  57. character array of length 1024, i.e. char(*)[1024]. This is not the
  58. same as char *.
  59.  
  60. done itself decays to char *.
  61.  
  62. (Never take the warnings from an unknown compiler too literally).
  63.  
  64. Cheers
  65. Tanmoy
  66. --
  67. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  68. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  69. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  70. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  71. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  72. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  73.